export default {
    encryptionMxr: function (data, boolEncryption) {
      const packetHeaderSize = 5
      var sndBuf = stringToByte(data)
      var size = sndBuf.length

      var tempBuffer = []
      if (boolEncryption) {
        do {
          var intRand = Math.random() * 255
          tempBuffer.push(parseInt(intRand))
        } while (tempBuffer[0] === 0)
      } else {
        tempBuffer.push(0)
      }

      var byteLength = int32ToBytes(size + packetHeaderSize)

      tempBuffer[1] = byteLength[0]
      tempBuffer[2] = byteLength[1]
      tempBuffer[3] = byteLength[2]
      tempBuffer[4] = byteLength[3]

      if (tempBuffer[0] !== 0) {
        for (var i = 0; i < size; i++) {
          tempBuffer[packetHeaderSize + i] = parseInt((sndBuf[i] + (i ^ (parseInt(tempBuffer[0])))) % 256)
          tempBuffer[packetHeaderSize + i] = parseInt((tempBuffer[packetHeaderSize + i] ^ (parseInt(tempBuffer[0]) ^ (size - i))) % 256)
        }
      } else {
        for (var i = 0; i < size; i++) {
          tempBuffer[packetHeaderSize + i] = sndBuf[i]
        }
      }

      var result = arrayBufferToBase64(tempBuffer)
      return result
    },

    decryptionMxr: function (data) {
      if (data === undefined || data === '') {
        return ''
      }
      const packetHeaderSize = 5

      var arrayBuffer = base64ToArrayBuffer(data.replace(/ /g, '+'))
      var buffer = new Uint8Array(arrayBuffer)
      var newBuffer = []
      var size = buffer.byteLength

      if (buffer[0] == '\0') {
        for (var i = packetHeaderSize; i < size; i++) {
          newBuffer[i - packetHeaderSize] = buffer[i]
        }

        return arrayBufferToBase64(newBuffer)
      }

      for (var i = packetHeaderSize; i < size; i++) {
        var t1 = parseInt((buffer[i] ^ (buffer[0] ^ (size - i))) % 256)

        if (t1 < 0) {
          t1 += 256
        }

        newBuffer[i - packetHeaderSize] = t1

        var t2 = parseInt((newBuffer[i - packetHeaderSize] - ((i - packetHeaderSize) ^ buffer[0])) % 256)

        if (t2 < 0) {
          t2 += 256
        }

        newBuffer[i - packetHeaderSize] = t2
      }

      var result = bytesToString(newBuffer)
      return result
    }
  }


function int32ToBytes(num) {
  var byte = []
  for (var i = 0; i < 4; i++) {
    byte[i] = num >> 24 - i * 8
  }
  return byte.reverse()
}

function bytesToString(arr) {
  if (typeof arr === 'string') {
    return arr
  }
  var str = '',
    _arr = arr
  for (var i = 0; i < _arr.length; i++) {
    var one = _arr[i].toString(2),
      v = one.match(/^1+?(?=0)/)
    if (v && one.length == 8) {
      var bytesLength = v[0].length
      var store = _arr[i].toString(2).slice(7 - bytesLength)
      for (var st = 1; st < bytesLength; st++) {
        store += _arr[st + i].toString(2).slice(2)
      }
      str += String.fromCharCode(parseInt(store, 2))
      i += bytesLength - 1
    } else {
      str += String.fromCharCode(_arr[i])
    }
  }
  return str
}

function arrayBufferToBase64(buffer) {
  var binary = ''
  var bytes = new Uint8Array(buffer)
  var len = bytes.byteLength
  for (var i = 0; i < len; i++) {
    binary += String.fromCharCode(bytes[i])
  }
  return window.btoa(binary)
}

function base64ToArrayBuffer(base64) {
  var binary_string = window.atob(base64)
  var len = binary_string.length
  var bytes = new Uint8Array(len)
  for (var i = 0; i < len; i++) {
    bytes[i] = binary_string.charCodeAt(i)
  }
  return bytes.buffer
}

function stringToByte(str) {
  var bytes = new Array()
  var len, c
  len = str.length
  for (var i = 0; i < len; i++) {
    c = str.charCodeAt(i)
    if (c >= 0x010000 && c <= 0x10FFFF) {
      bytes.push(((c >> 18) & 0x07) | 0xF0)
      bytes.push(((c >> 12) & 0x3F) | 0x80)
      bytes.push(((c >> 6) & 0x3F) | 0x80)
      bytes.push((c & 0x3F) | 0x80)
    } else if (c >= 0x000800 && c <= 0x00FFFF) {
      bytes.push(((c >> 12) & 0x0F) | 0xE0)
      bytes.push(((c >> 6) & 0x3F) | 0x80)
      bytes.push((c & 0x3F) | 0x80)
    } else if (c >= 0x000080 && c <= 0x0007FF) {
      bytes.push(((c >> 6) & 0x1F) | 0xC0)
      bytes.push((c & 0x3F) | 0x80)
    } else {
      bytes.push(c & 0xFF)
    }
  }
  return bytes
}
